part of '../repository.dart';

const _kAppStorePurchasedSubscriptionOriginalTransactionIdKey = 'original_transaction_id';
const _kAppStorePurchasedSubscriptionAutoRenewalEnabledKey = 'auto_renewal_status';
const _kAppStorePurchasedSubscriptionStatusKey = 'status';
const _kAppStorePurchasedSubscriptionGracePeriodExpirationTimeKey = 'grace_period_expiration_time';
const _kAppStorePurchasedSubscriptionExpirationTimeKey = 'expiration_time';

const _kAppStorePurchasedSubscriptionTypeValue = 'purchased_subscription';

class _AppStorePurchasedSubscriptionDto extends Equatable implements _AppStorePurchasedItemDto {
  const _AppStorePurchasedSubscriptionDto._internal({
    required this.purchaseItemSku,
    required this.originalTransactionId,
    required this.autoRenewalEnabled,
    required this.statusDto,
    required this.gracePeriodExpirationTimeDto,
    required this.expirationTimeDto,
  });

  @override
  final String purchaseItemSku;

  final String originalTransactionId;
  final bool autoRenewalEnabled;
  final _AppStorePurchasedSubscriptionStatusDto statusDto;
  final ApiTimeDto? gracePeriodExpirationTimeDto;
  final ApiTimeDto? expirationTimeDto;

  @override
  List<Object?> get props {
    return [
      purchaseItemSku,
      originalTransactionId,
      autoRenewalEnabled,
      statusDto,
      gracePeriodExpirationTimeDto,
      expirationTimeDto,
    ];
  }

  factory _AppStorePurchasedSubscriptionDto.fromJson(Map<String, dynamic> json) {
    final statusString = json[_kAppStorePurchasedSubscriptionStatusKey] as String;
    final statusDto = _AppStorePurchasedSubscriptionStatusDto.fromString(statusString);

    final gracePeriodExpirationTimeString =
        json[_kAppStorePurchasedSubscriptionGracePeriodExpirationTimeKey] as String?;

    ApiTimeDto? gracePeriodExpirationTimeDto;

    if (gracePeriodExpirationTimeString != null) {
      gracePeriodExpirationTimeDto = ApiTimeDto.fromString(gracePeriodExpirationTimeString);
    }

    final expirationTimeString = json[_kAppStorePurchasedSubscriptionExpirationTimeKey] as String?;

    ApiTimeDto? expirationTimeDto;

    if (expirationTimeString != null) {
      expirationTimeDto = ApiTimeDto.fromString(expirationTimeString);
    }

    return _AppStorePurchasedSubscriptionDto._internal(
      purchaseItemSku: json[_kAppStorePurchasedItemPurchaseItemSkuKey],
      originalTransactionId: json[_kAppStorePurchasedSubscriptionOriginalTransactionIdKey],
      autoRenewalEnabled: json[_kAppStorePurchasedSubscriptionAutoRenewalEnabledKey],
      statusDto: statusDto,
      gracePeriodExpirationTimeDto: gracePeriodExpirationTimeDto,
      expirationTimeDto: expirationTimeDto,
    );
  }

  @override
  AppStorePurchasedSubscription toEntity() {
    return AppStorePurchasedSubscription(
      purchaseItemSku: purchaseItemSku,
      originalTransactionId: originalTransactionId,
      autoRenewalEnabled: autoRenewalEnabled,
      status: statusDto.toEntity(),
      gracePeriodExpirationTime: gracePeriodExpirationTimeDto?.toEntity(),
      expirationTime: expirationTimeDto?.toEntity(),
    );
  }
}
